package com.lukstron.youko;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		_populateMode();
		_populateMethod();
		_bindTest();
		_bindMeasure();
		_readSetting();
		_bindMode();
		_bindMethod();
		_bindElectrode();
		_bindSpacing();
		_bindLimit();
	}

	protected void _populateMode() {
		ArrayAdapter<CharSequence> adapter = null;
		int arrayId = R.array.modes;
		int viewId = android.R.layout.simple_spinner_item;
		adapter = ArrayAdapter.createFromResource(this, arrayId, viewId);
		int resource = android.R.layout.simple_spinner_dropdown_item;
		adapter.setDropDownViewResource(resource);
		Spinner spinner = (Spinner) findViewById(R.id.mode);
		spinner.setAdapter(adapter);
	}

	protected void _populateMethod() {
		ArrayAdapter<CharSequence> adapter = null;
		int arrayId = R.array.methods;
		int viewId = android.R.layout.simple_spinner_item;
		adapter = ArrayAdapter.createFromResource(this, arrayId, viewId);
		int resource = android.R.layout.simple_spinner_dropdown_item;
		adapter.setDropDownViewResource(resource);
		Spinner s = (Spinner) findViewById(R.id.method);
		s.setAdapter(adapter);
	}

	protected void _bindMeasure() {
		Button button = (Button) findViewById(R.id.measure);
		OnClickListener listener = null;
		listener = new OnClickListener() {
			public void onClick(View v) {
				_writeSetting();
				Intent intent = new Intent(SettingActivity.this,
						MeasureActivity.class);
				startActivity(intent);
			}
		};
		button.setOnClickListener(listener);
	}

	protected void _bindTest() {
		Button button = (Button) findViewById(R.id.test);
		OnClickListener listener = null;
		listener = new OnClickListener() {
			public void onClick(View v) {
				_writeSetting();
				Intent intent = null;
				intent = new Intent(SettingActivity.this, TestActivity.class);
				startActivity(intent);
			}
		};
		button.setOnClickListener(listener);
	}

	protected void _readSetting() {
		Integer integer = null;
		Float f = null;
		SharedPreferences pref = null;
		Spinner spinner = null;
		EditText editText = null;

		pref = getSharedPreferences("GeoRes", Context.MODE_PRIVATE);

		spinner = (Spinner) findViewById(R.id.mode);
		integer = pref.getInt("mode", 0);
		spinner.setSelection(integer);

		spinner = (Spinner) findViewById(R.id.method);
		integer = pref.getInt("method", 0);
		spinner.setSelection(integer);

		editText = (EditText) findViewById(R.id.electrode);
		integer = pref.getInt("electrode", 16);
		editText.setText(integer.toString());

		editText = (EditText) findViewById(R.id.spacing);
		integer = pref.getInt("spacing", 1);
		editText.setText(integer.toString());

		editText = (EditText) findViewById(R.id.limit);
		integer = pref.getInt("limit", 1);
		editText.setText(integer.toString());

		editText = (EditText) findViewById(R.id.duration);
		integer = pref.getInt("duration", 4);
		editText.setText(integer.toString());

		editText = (EditText) findViewById(R.id.delay);
		f = pref.getFloat("delay", (float) 0.5);
		editText.setText(f.toString());

		editText = (EditText) findViewById(R.id.cycle);
		integer = pref.getInt("cycle", 1);
		editText.setText(integer.toString());

		editText = (EditText) findViewById(R.id.retry);
		integer = pref.getInt("retry", 5);
		editText.setText(integer.toString());
	}

	protected void _writeSetting() {
		String string = null;
		Integer integer = null;
		Float f = null;
		SharedPreferences pref = null;
		Spinner spinner = null;
		EditText editText = null;

		pref = getSharedPreferences("GeoRes", Context.MODE_PRIVATE);
		Editor editor = pref.edit();

		spinner = (Spinner) findViewById(R.id.mode);
		integer = spinner.getSelectedItemPosition();
		editor.putInt("mode", integer);

		spinner = (Spinner) findViewById(R.id.method);
		integer = spinner.getSelectedItemPosition();
		editor.putInt("method", integer);

		editText = (EditText) findViewById(R.id.electrode);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("electrode", integer);

		editText = (EditText) findViewById(R.id.spacing);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("spacing", integer);

		editText = (EditText) findViewById(R.id.limit);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("limit", integer);

		editText = (EditText) findViewById(R.id.duration);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("duration", integer);

		editText = (EditText) findViewById(R.id.delay);
		string = editText.getText().toString();
		f = Float.parseFloat(string);
		editor.putFloat("delay", f);

		editText = (EditText) findViewById(R.id.cycle);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("cycle", integer);

		editText = (EditText) findViewById(R.id.retry);
		string = editText.getText().toString();
		integer = Integer.parseInt(string);
		editor.putInt("retry", integer);

		editor.commit();
	}

	protected TextWatcher _updateTextWatcher = new TextWatcher() {

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void afterTextChanged(Editable s) {
			_update(false);
		}

	};

	protected TextWatcher _electrodeTextWatcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
			_update(true);
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};

	protected OnItemSelectedListener _listener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			_update(true);
		}

		public void onNothingSelected(AdapterView<?> arg0) {

		}

	};

	/**
	 * Update information when mode, method, electrode, spacing or limit
	 * changes.
	 */
	protected void _update(Boolean updateLimit) {
		// Reset view
		TextView nadir = (TextView) findViewById(R.id.nadir);
		nadir.setText("");
		TextView max = (TextView) findViewById(R.id.max);
		max.setText("");
		TextView depth = (TextView) findViewById(R.id.depth);
		depth.setText("");
		TextView maxLimit = (TextView) findViewById(R.id.max_limit);
		maxLimit.setText("");
		TextView depthLimit = (TextView) findViewById(R.id.depth_limit);
		depthLimit.setText("");

		Spinner spinner = null;
		EditText editText = null;
		StringBuilder stringBuilder = null;
		String string = null;
		Integer integer = null;
		Integer electrode = null;
		Integer spacing = null;

		editText = (EditText) findViewById(R.id.electrode);
		string = editText.getText().toString();
		try {
			electrode = Integer.parseInt(string);
		} catch (Exception e) {
			electrode = 0;
		}

		if (electrode < 4) {
			return;
		}

		spinner = (Spinner) findViewById(R.id.method);
		integer = spinner.getSelectedItemPosition();

		IMethod method = null;
		if (integer == 0) {
			method = new Shlumberger(electrode);
		} else {
			method = new Wenner(electrode);
		}

		stringBuilder = new StringBuilder();
		stringBuilder.append(method.getNadir());
		nadir.setText(stringBuilder.toString());

		stringBuilder = new StringBuilder();
		stringBuilder.append(method.count());
		max.setText(stringBuilder.toString());

		editText = (EditText) findViewById(R.id.spacing);
		string = editText.getText().toString();
		try {
			spacing = Integer.parseInt(string);
		} catch (Exception e) {
			spacing = 0;
		}

		if (spacing < 1) {
			return;
		}

		stringBuilder = new StringBuilder();
		stringBuilder.append(method.getNadir() * spacing);
		depth.setText(stringBuilder.toString());

		editText = (EditText) findViewById(R.id.limit);

		if (updateLimit) {
			editText.setText(Integer.toString(method.getNadir()));
			return;
		}

		string = editText.getText().toString();
		try {
			integer = Integer.parseInt(string);
		} catch (Exception e) {
			integer = 0;
		}

		if (integer < 1 || integer > method.getNadir()) {
			return;
		}

		stringBuilder = new StringBuilder();
		stringBuilder.append(method.count(integer));
		maxLimit.setText(stringBuilder.toString());

		stringBuilder = new StringBuilder();
		stringBuilder.append(integer * spacing);
		depthLimit.setText(stringBuilder.toString());
	}

	protected void _bindMode() {
		Spinner spinner = (Spinner) findViewById(R.id.mode);
		spinner.setOnItemSelectedListener(_listener);
	}

	protected void _bindMethod() {
		Spinner spinner = (Spinner) findViewById(R.id.method);
		spinner.setOnItemSelectedListener(_listener);
	}

	protected void _bindElectrode() {
		EditText editText = (EditText) findViewById(R.id.electrode);
		editText.addTextChangedListener(_electrodeTextWatcher);
	}

	protected void _bindSpacing() {
		EditText editText = (EditText) findViewById(R.id.spacing);
		editText.addTextChangedListener(_updateTextWatcher);
	}

	protected void _bindLimit() {
		EditText editText = (EditText) findViewById(R.id.limit);
		editText.addTextChangedListener(_updateTextWatcher);
	}

}
